$(document).ready(function () {
	$(".btn_theme1").bind("touchstart",function(){
		$(this).css("background-color", "#CC6633");
	});

	$(".btn_theme1").bind("touchend",function(){
		$(this).css("background-color", "#FF6633");
	});

	
	$("div.select").bind("touchstart",function(){
		$(this).css("background-color", "#ddd");
	});

	$("div.select").bind("touchend",function(){
		$(this).css("background-color", "#fff");
	});


	$("span.a").click(function(){
		var href = $(this).attr("href");
		if(href != null && href != ""){
			window.location.href = href;
		}
	});

	$("input[type='number']").keyup(function(){
		var value = $(this).val();

		var exp = new RegExp("^([0-9]*[.0-9])$");
		//是不是小数？
		if(!exp.test(value)){
			$(this).val(Math.floor(value*100)/100);		
		}

		if(value < 0){
			$(this).val(Math.abs(value));	
		}
	});

	$("input[type='number']").blur(function(){
		var value = $(this).val();
		$(this).val(Math.floor(value*100)/100);		
	});

	$("div.select").each(function(index, element){	
		var objSelect = $(this);		
		var flag_selected = false;
		$(objSelect).find("span.option").each(function(index, element){			
			if($(this).css("display") == "block"){
				$(objSelect).attr("value", $(this).attr("value"));		
				$(objSelect).attr("type", $(this).attr("type"));
				flag_selected = true;
			}

			if(index == $(objSelect).find("span.option").length - 1){
				if(!flag_selected){
					$(objSelect).attr("value", $(objSelect).find("span.option:first").attr("value"));		
					$(objSelect).attr("type", $(objSelect).find("span.option:first").attr("type"));
					$(objSelect).find("span.option:first").css("display", "block");
				}
			}
		}); 	
	});
	

	$("div.select").click(function(){
		var objDIV = this;
		var arrAccounts = new Array();
		var items = $(this).find("span.option");
		$(items).each(function(index, element){
			var data = new Array();
			data["value"] = $(this).attr("value");
			data["text"] = $(this).text();

			if($(this).css("display") == "block"){			
				data["selected"] = "true";
			}
			arrAccounts.push(data);
		}); 

		if(arrAccounts.length <1 ){
			return;
		}

		ui.option(arrAccounts, function(data){
			//遍历其下所有option, 将相同的显示，其他隐藏
			$(items).each(function(index, element){
				if($(this).attr("value") == data){			
					$(this).css("display", "block");		
					$(objDIV).attr("type", $(this).attr("type"));			
					$(objDIV).attr("value", $(this).attr("value"));
					
					$(objDIV).trigger('change', {"value":data, "type":$(this).attr("type")});
				}else{		
					$(this).css("display", "none");
				}
			}); 
		});
	});
});

var styleForm = "display:none; position:fixed; top:150px; left:6%; overflow:hidden; z-index:997; background-color:#fff; border:#ccc 1px solid; box-shadow:0 0 10px #666; border-radius: 10px;";
var styleTitle = "float:left; width:100%; height:30px; line-height:30px; overflow:hidden; font-size:1.1em; color:#666; text-align:left; font-weight:bold;";
var styleButton = "height:45px; width:100%; border:0px solid #dddddd; background-color:#fff; color:#333; outline:none; ";
var styleHidelayer = "position:absolute; top:0px; left:0px; background-color:rgba(116,116,116,0.6); z-index:996; display:none;";/*filter:alpha(opacity=60); opacity:0.60; */

var ui = function(){};

ui.alert = function(title, text, callbackOk){
	eleDIV = document.createElement("div");
	eleDIV.setAttribute("id", "eleDIV");
	eleDIV.innerHTML = 
		'<div id="myAlertForm" style="width:88%; height:auto; ' + styleForm + '">'+

		'	<div style="float:left; width:100%; padding:10px 15px; border-bottom:thin solid #e5e5e5; overflow:hidden;">'+
		'	    <span style="' + styleTitle + '">' + title + ':</span>' +	 
		'	</div>'+

		'	<div id="player" style="float:left; width:100%; padding:20px 15px; height:auto;">' + text + 	
		'	</div>'+

		'	<div style="float:left; width:100%; padding:0px; border-top:1px solid #e5e5e5; text-align:right">'+
		'	    <input type="button" id="DIALOG_OK" style="' + styleButton + '" value="确定">' +
		'	</div>'+

		'</div>'+
		'<div id="hidelayer" style="' + styleHidelayer + '">'+
		'</div>';	
	document.body.appendChild(eleDIV);

	document.getElementById("hidelayer").style.width = document.body.clientWidth + 'px';
	document.getElementById("hidelayer").style.height = document.body.scrollHeight + 'px';
	document.getElementById("hidelayer").style.display = "block";				
	document.getElementById("myAlertForm").style.display = "block";
    
	document.getElementById("DIALOG_OK").onclick=function (){ 		
		$(eleDIV).fadeOut("slow" ,function(){
			document.body.removeChild(eleDIV);
			callbackOk();	
		});		
	};

	document.getElementById("hidelayer").onclick=function (){ 		
		$(eleDIV).fadeOut("slow" ,function(){
			document.body.removeChild(eleDIV);
			callbackOk();	
		});		
	};

	document.getElementById("DIALOG_OK").addEventListener("touchstart", function(){
		this.style.backgroundColor = "#ddd";
	}, false);

	document.getElementById("DIALOG_OK").addEventListener("touchend", function(){
		this.style.backgroundColor = "#fff";
	}, false);
}


var styleButtonOK = "float:left; height:45px; width:50%; border:0px solid #dddddd; background-color:#fff; color:#333; outline:none; ";
ui.confirm = function(title, text, callbackOk){
	eleDIV = document.createElement("div");
	eleDIV.setAttribute("id", "eleDIV");
	eleDIV.innerHTML = 
		'<div id="myConfirmForm" style="width:88%; height:auto; ' + styleForm + '">'+

		'	<div style="float:left; width:100%; padding:10px 15px; border-bottom:thin solid #e5e5e5; overflow:hidden;">'+
		'	    <span style="' + styleTitle + '">' + title + ':</span>' +	 
		'	</div>'+

		'	<div id="player" style="float:left; width:100%; padding:20px 15px; height:auto;">' + text + 	
		'	</div>'+

		'	<div style="float:left; width:100%; padding:0px; border-top:1px solid #e5e5e5; text-align:right">'+
		'	    <input type="button" id="DIALOG_OK" style="' + styleButtonOK + '" value="确定">' +
		'	    <input type="button" id="DIALOG_CANCEL" style="' + styleButtonOK + '" value="取消">' +
		'	</div>'+

		'</div>'+
		'<div id="hidelayer" style="' + styleHidelayer + '">'+
		'</div>';	
	document.body.appendChild(eleDIV);

	document.getElementById("hidelayer").style.width = document.body.clientWidth + 'px';
	document.getElementById("hidelayer").style.height = document.body.scrollHeight + 'px';
	document.getElementById("hidelayer").style.display = "block";				
	document.getElementById("myConfirmForm").style.display = "block";
    
	document.getElementById("DIALOG_OK").onclick=function (){ 		
		$(eleDIV).fadeOut("slow" ,function(){
			document.body.removeChild(eleDIV);
			callbackOk();	
		});		
	};

	document.getElementById("DIALOG_CANCEL").onclick=function (){ 		
		$(eleDIV).fadeOut("slow" ,function(){
			document.body.removeChild(eleDIV);
			//callbackOk();	
		});		
	};

	document.getElementById("hidelayer").onclick=function (){ 		
		$(eleDIV).fadeOut("slow" ,function(){
			document.body.removeChild(eleDIV);
			//callbackOk();	
		});		
	};

	document.getElementById("DIALOG_OK").addEventListener("touchstart", function(){
		this.style.backgroundColor = "#ddd";
	}, false);

	document.getElementById("DIALOG_OK").addEventListener("touchend", function(){
		this.style.backgroundColor = "#fff";
	}, false);

	document.getElementById("DIALOG_CANCEL").addEventListener("touchstart", function(){
		this.style.backgroundColor = "#ddd";
	}, false);

	document.getElementById("DIALOG_CANCEL").addEventListener("touchend", function(){
		this.style.backgroundColor = "#fff";
	}, false);
}

var styleToastForm = "display:none; position:fixed; bottom:50px; width:50%; left:25%; height:auto; overflow:hidden; z-index:998; background-color:rgba(0,0,0,1); border:#ccc 0px solid; box-shadow:0 0 0px #666; border-radius: 6px;";
ui.toast = function(text, callbackOk){
	eleDIV = document.createElement("div");
	eleDIV.setAttribute("id", "eleDIV");
	eleDIV.innerHTML = 
		'<div id="myToastForm" style="' + styleToastForm + '">'+

		'	<div id="player" style="float:left; width:100%; padding:10px 15px; height:auto; color:#fff; text-align:center">' + text +
		'	</div>'+

		'</div>'+

		'<div id="hidelayer" style="' + styleHidelayer + '">'+
		'</div>';
	document.body.appendChild(eleDIV);

	document.getElementById("hidelayer").style.width = document.body.clientWidth + 'px';
	document.getElementById("hidelayer").style.height = document.body.scrollHeight + 'px';
	document.getElementById("hidelayer").style.display = "block";
	document.getElementById("myToastForm").style.display = "block";

	setTimeout(function(){	
		document.body.removeChild(eleDIV);
		callbackOk();
	}, 2000);
}

var styleShowWaitingToastForm = "display:none; position:fixed; top:0px; width:50%; left:25%; height:auto; overflow:hidden; z-index:998; background-color:rgba(0,0,0,1); border:#ccc 0px solid; box-shadow:0 0 0px #666; border-radius: 6px;";
ui.showWaitingToast = function(text, callbackOk){
	eleDIV = document.createElement("div");
	eleDIV.setAttribute("id", "eleDIV");
	eleDIV.innerHTML = 
		'<div id="showWaitingToastForm" style="' + styleShowWaitingToastForm + '">'+

		'	<div id="player" style="float:left; width:100%; padding:10px 15px; height:auto; color:#fff; text-align:center">' + text +
		'	</div>'+

		'</div>'+

		'<div id="hidelayer" style="' + styleHidelayer + '">'+
		'</div>';
	document.body.appendChild(eleDIV);

	document.getElementById("hidelayer").style.width = document.body.clientWidth + 'px';
	document.getElementById("hidelayer").style.height = document.body.scrollHeight + 'px';
	document.getElementById("hidelayer").style.display = "block";

	document.getElementById("showWaitingToastForm").style.top = (document.body.clientHeight/2-30) + 'px';
	document.getElementById("showWaitingToastForm").style.display = "block";

	//setTimeout(function(){	
	//	document.body.removeChild(eleDIV);
	//	callbackOk();
	//}, 1000);
}

ui.closeWaiting = function(callbackOk){
	var eleDIV = document.getElementById("eleDIV");
	document.body.removeChild(eleDIV);
}

/*
var styleOptionForm = "display:none; position:fixed; top:100px; width:80%; left:10%; height:auto; max-height:320px; OVERFLOW-Y:auto; OVERFLOW-X:hidden; z-index:999; background-color:#336; border:#ccc 0px solid; box-shadow:0 0 10px #666; border-radius: 0px;";

ui.option = function(values, callbackOk){
	eleDIV = document.createElement("div");
	eleDIV.setAttribute("id", "eleDIV");
	
	strInnerHtml = '<div id="myOptionForm" style="' + styleOptionForm + '">';
	strInnerHtml += '	<div id="player" style="float:left; width:100%; padding:0px 15px; height:40px; line-height:40px; background-color:#fff; color:#336;; border-bottom:2px solid #336" >请选择</div>';
	for(var i = 0; i < values.length; i++) { 
		if(values[i]["selected"] == "true"){
			strInnerHtml += '	<div id="player"  value="' + values[i]["value"] + '" style="float:left; width:100%; padding:0px 15px; height:40px; line-height:40px; color:#fff">' + values[i]["text"] + '</div>';	
			break;
		}
	} 
	for(var i = 0; i < values.length; i++) { 
		if(values[i]["selected"] != "true"){
			strInnerHtml += '	<div id="player" name="DIV12345678" value="' + values[i]["value"] + '" style="float:left; width:100%; padding:0px 15px; height:40px; line-height:40px; background-color:#fff; color:#336; border-top:1px solid #eee">' + values[i]["text"] + '</div>';
		}
	} 
	strInnerHtml += '</div>';

	strInnerHtml += '<div id="hidelayer" style="' + styleHidelayer + '">';
	strInnerHtml += '</div>';

	eleDIV.innerHTML = strInnerHtml;
	document.body.appendChild(eleDIV);

	document.getElementById("hidelayer").style.width = document.body.clientWidth + 'px';
	document.getElementById("hidelayer").style.height = document.body.scrollHeight + 'px';
	document.getElementById("hidelayer").style.display = "block";
	document.getElementById("myOptionForm").style.display = "block";

	document.getElementById("hidelayer").onclick=function (){ 		
		//$(eleDIV).fadeOut("slow" ,function(){
			document.body.removeChild(eleDIV);
			//callbackOk();	
		//});		
	};

	$("div[name='DIV12345678']").click(function(){
		callbackOk($(this).attr("value"));
		//$(eleDIV).fadeOut("slow" ,function(){
			document.body.removeChild(eleDIV);
		//});	
	});
}
*/